package com.vtb.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vtb.base.VtbException;
import com.vtb.entity.SsOauth;
import com.vtb.inter.VtbDaoInterface;
import com.vtb.utils.JdbcUtils;

/**
 * SdDao 实现类
 *
 * @author Chen Tao
 * @date 2012-7-1 下午05:40:27
 */
public class SsDaoImpl implements VtbDaoInterface<SsOauth, SsOauth> {
    private static final Log log = LogFactory.getLog(SsDaoImpl.class);
    @Override
    public void del(SsOauth ssOauth) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "delete from ssdata where uni=?";
            qr.update(JdbcUtils.getConnection(), sql, ssOauth.getUni());
        } catch (SQLException e) {
            log.error("删除ssdata错误：", e);
            throw new VtbException("删除ssdata错误"+e);
        }
        
    }

    @SuppressWarnings("deprecation")
    @Override
    public SsOauth get(SsOauth ssOauth) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "select * from ssdata where uni=?";
            ssOauth = (SsOauth) qr.query(JdbcUtils.getConnection(), sql,ssOauth.getUni(),new BeanHandler(SsOauth.class));
            return ssOauth;
        } catch (SQLException e) {
            log.error("通过ssoauth查找ssdata错误：", e);
            throw new VtbException("通过ssoauth查找ssdata错误"+e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SsOauth> getAll() {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "select * from ssdata";
            return (List<SsOauth>) qr.query(JdbcUtils.getConnection(), sql,new BeanListHandler(SsOauth.class));
        } catch (SQLException e) {
            log.error("查询ssdata所有信息：", e);
            throw new VtbException("查询ssdata所有信息"+e);
        }
    }

    @Override
    public void insert(SsOauth ssOauth) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "insert into ssdata(uni,access_token,expires_in,createTime,refr,refq,s2q,s2r,nick) values (?,?,?,?,?,?,?,?,?)";
            Object[] params = {ssOauth.getUni(),ssOauth.getAccess_token(),ssOauth.getExpires_in(),ssOauth.getCreateTime(),ssOauth.getRefr(),ssOauth.getRefq(),ssOauth.getS2q(),ssOauth.getS2r(),ssOauth.getNick()};
            qr.update(JdbcUtils.getConnection(), sql, params);
        } catch (SQLException e) {
            log.error("插入ssdata错误：", e);
            throw new VtbException("插入ssdata错误"+e);
        }
        
    }

    @Override
    public void update(SsOauth ssOauth) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "update ssdata set access_token=?,expires_in=?,createTime=?,refr=?,ref2=?,s2q=?,s2r=? where uni=?";
            Object[] params = {ssOauth.getAccess_token(),ssOauth.getExpires_in(),ssOauth.getCreateTime(),ssOauth.getRefr(),ssOauth.getRefq(),ssOauth.getS2r(),ssOauth.getS2q(),ssOauth.getUni()};
            qr.update(JdbcUtils.getConnection(), sql, params);
        } catch (SQLException e) {
            log.error("更新ssdata错误：", e);
            throw new VtbException("更新ssdata错误"+e);
        }
        
    }
    @Override
    public List<SsOauth> getAllInfo() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void insertAll(SsOauth k) {
        // TODO Auto-generated method stub
        
    }
}
